<?php
/**
 * 《帐号管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Country.class.php 832 2010-01-06 09:17:14Z xuy $
 */
require_once(dirname(__FILE__) . '/WifiUtil.class.php');

class Log extends WifiUtil {
	public $tbl = Constant::tbl_wifi_log;
	
	public $name = array(
		'-' => '请选择',
		'ns12' => '南山办公区',
		'sj12' => '沙井办公区',
		'sj60' => '沙井宿舍',
		'qx12' => '清溪办公区',
		'qx20' => '清溪宿舍',
	);

	public $status = array(
		'-' => '请选择',
		'1' => '在线',
		'2' => '离线',
			
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',

		'gw_id' => '节点',
		'uid' => '帐号',
			
		'ip' => 'IP',
		'mac' => 'mac',
		
		'bytes' => '流量',
		'outgoing' => '上传',
		'incoming' => '下载',

		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	public $groupby = array(
		'-' => '请选择',
		
		'gw_id' => '节点',
		'uid' => '帐号',
		'ip' => 'ip',
		'mac' => 'mac',

		'status' => '状态',
		'1' => '小时',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('name_option', $this->name);
	}

	function tab($controller, $request) {
		$data = array(
			'帐号管理' => array('cc'=>'radius.php?do=Radcheck.find', 'js'=>'1'),
			'认证信息' => array('cc'=>'radius.php?do=Radpost.find', 'js'=>'1'),
			'计费信息' => array('cc'=>'radius.php?do=Radacct.find', 'js'=>''),
		);
	
		$ret = $this->_tab($data);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'tab');
		return true;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
		
		$controller->assign('t1', strtotime("-3 day"));
		
		$controller->assign('pagesize_s', '100');

		$controller->display($request, 'wifi/log/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}
		
		if ($px == 'ip') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['ip'];
		}
		
		if ($px == 'outgoing') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = printsize($row['outgoing']);
		}
		
		if ($px == 'incoming') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = printsize($row['incoming']);
		}
		
		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($this->_audit('111')) {
			return true;
		}

		if ($row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
	
		$emp = $_SESSION['id'];
	
		$p = array('1'=>'209', '2'=>'210', '3'=>'211');
	
		$a = $this->_audit_office($p);
		if (count($a) > 0) {
			$office = implode(",", $a);
	
			return sprintf(" (office in (%s) OR emp = '%s')", $office, $emp);
		}
	
		$dept = $this->_where_dept($emp);
		if (!empty($dept)) {
			return sprintf(" (%s)", $dept);
		}
	
		return sprintf(" (emp = '%s')", $emp);
	}
	
	function _where($request) {
		$uid_s = $request['uid_s'];
		$name_s = $request['name_s'];
		$mac_s = $request['mac_s'];
		
		$my = $this->_my();
		
		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
		
		if (!empty($uid_s)) {
			$where .= " AND uid = '" . $uid_s . "'";
		}
		
		if (!empty($mac_s)) {
			$where .= " AND mac = '" . $mac_s . "'";
		}
		
		if (!empty($name_s)) {
			$where .= sprintf(" AND (gw_id LIKE '%s' OR ip LIKE '%s' OR mac LIKE '%s')", '%' . $name_s . '%', '%' . $name_s . '%', '%' . $name_s . '%');
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);
		
		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}

			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'wifi/log/list');
		return true;
	}
	
	function xls($controller, $request) {
		$this->pretreat($controller);
	
		$this->findPara($controller, $request);
	
		$request['tpl'] = "xls";
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);
	
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
	
			$row = $this->_orderby_x($controller, $row, $this->orderby_s);
				
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
	
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);
	
		$controller->display($request, 'wifi/log/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'wifi/log/edit');
		return true;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
	
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
	
		$date = $request['date'];
		$ip = $request['ip'];
		$timeout = $request['timeout'];
		$online = $request['online'];
	
		$name = addslashes($request['name']);
		$content = addslashes($request['content']);
		
		$token = $request['token'];
		$status = $request['status'];
	
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET token = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $token, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'wifi/log/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['time'] = strtotime($row['modifytime']) - strtotime($row['createtime']);
		
		$row['mac_x'] = $this->_emp_by_mac($row['mac']);
	
		$row = $this->_view_t($row);
	
		return $row;
	}
	
	function _by_mac($db, $mac) {
		
		// $db = newdb("abc");
		$fmt = "SELECT * FROM %s WHERE mac = '%s'";
		$sql = sprintf($fmt, $this->tbl, $mac);
		echo $sql . "\n";
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			return $row;
		}
		
		return false;
	}
	
	function group($controller, $request) {
		$this->pretreat_group($controller);
	
		$controller->assign('t1', time());
		// $controller->assign('t2', strtotime('+1 day'));
	
		$num = array(
			'1' => '按次数',
			'2' => '按流量',
		);
		$controller->assign('num_option', $num);
	
		$controller->display($request, 'wifi/connection/group');
		return true;
	}
	
	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'gw_id');
	
		$controller->assign('groupby', $this->groupby[$px]);
	
		return $this->groupby_dd($px);
	}
	
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);
	
		$num_s = $request['num_s'];
		if (!empty($num_s) && $num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(bytes) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, COUNT(*) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->tbl);
	
		$where = $this->_where($request);
	
		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);
	
		$this->_group($controller, $request);
	
		// $controller->display($request, 'list');
		return true;
	}
	
	function _group($controller, $request) {
	
		$sql = $request['sql'];
	
		$this->_group2($controller, $sql);
	
		$tpl = $request['tpl'];
	
		if ($tpl == 'xls') {
			$filename = sprintf("%s.xls", date("YmdHis"));
			header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);
		}
	
		if ($tpl == 'doc') {
			$filename = sprintf("%s.doc", date("YmdHis"));
			header('Content-Disposition:application/msword; filename=' . $filename);
		}
	
		$view = $request['view'];
		if ($view == 'print') {
			$controller->display($request, 'list_2');
		} else {
			$controller->display($request, 'wifi/connection/group_list');
		}
	}
	
	function _name($row, $px) {
		if (empty($px) || $px == '-') {
			$px = 'gw_id';
		}
	
		$row = $this->_name_t($row, $px);
	
		return $row;
	}
	
	function online($controller, $request) {
		$this->pretreat($controller);
	
		$dd = date("Y-m-d H:i:s", strtotime("-30 minute"));
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET status = '2' WHERE status = '1' AND modifytime < '%s'";
		$sql = sprintf($fmt, $this->tbl, $dd);
		echo $sql . "\n";
		$db->query($sql);
		
		require_once(dirname(__FILE__) . '/Node.class.php');
		$aa = new Node();
		
		$fmt = "SELECT gw_id, COUNT(*) AS online FROM %s WHERE status = '1' GROUP BY gw_id";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$aa->_online($row['gw_id'], $row['online']);
		}
	
		logdate($controller, $request);
		return true;
	}
	
	function _user_login($dd, $node) {
	
		$ff = "DATE_FORMAT(createtime, '%Y-%m-%d')";
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT COUNT(DISTINCT uid) AS c FROM %s WHERE gw_id = '%s' AND %s = '%s'";
		$sql = sprintf($fmt, $this->tbl, $node, $ff, $dd);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			return $row['c'];
		}
	
		return 0;
	}

	function t($controller, $request) {
		$this->pretreat($controller);

		echo substr("0755-89809672", 5);

		logdate($controller, $request);
		return true;
	}
}
?>